/*
 * ListenerConfigPanel.java
 *
 * Created on May 18, 2008, 8:46 PM
 */

package pl.edu.agh.ssm.monitor.gui;

import javax.swing.SpinnerNumberModel;

/**
 *
 * @author  aneezka
 */
public class ListenerConfigPanel extends javax.swing.JPanel {
    
    private static final long serialVersionUID = 1L;
	
    /** Creates new form ListenerConfigPanel */
    public ListenerConfigPanel(String listenerType) {
        initComponents();
        listenerCheckBox.setText(listenerType);
        this.type = listenerType;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        listenerCheckBox = new javax.swing.JCheckBox();
        ipLabel = new javax.swing.JLabel();
        lowerPortLabel = new javax.swing.JLabel();
        ipField = new javax.swing.JTextField();
        lowerPortSpinner = new javax.swing.JSpinner();
        higherPortLabel = new javax.swing.JLabel();
        higherPortSpinner = new javax.swing.JSpinner();

        setName("Form"); // NOI18N

        listenerCheckBox.setSelected(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pl.edu.agh.ssm.monitor.SessionMonitorApp.class).getContext().getResourceMap(ListenerConfigPanel.class);
        listenerCheckBox.setText(resourceMap.getString("listenerCheckBox.text")); // NOI18N
        listenerCheckBox.setName("listenerCheckBox"); // NOI18N
        listenerCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listenerCheckBoxActionPerformed(evt);
            }
        });

        ipLabel.setText(resourceMap.getString("ipLabel.text")); // NOI18N
        ipLabel.setName("ipLabel"); // NOI18N

        lowerPortLabel.setText(resourceMap.getString("lowerPortLabel.text")); // NOI18N
        lowerPortLabel.setName("lowerPortLabel"); // NOI18N

        ipField.setText(resourceMap.getString("ipField.text")); // NOI18N
        ipField.setName("ipField"); // NOI18N

        lowerPortSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 65535, 1));
        lowerPortSpinner.setName("lowerPortSpinner"); // NOI18N
        lowerPortSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                lowerPortSpinnerStateChanged(evt);
            }
        });

        higherPortLabel.setText(resourceMap.getString("higherPortLabel.text")); // NOI18N
        higherPortLabel.setName("higherPortLabel"); // NOI18N

        higherPortSpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 65535, 1));
        higherPortSpinner.setName("higherPortSpinner"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listenerCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ipLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ipField, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lowerPortLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lowerPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(higherPortLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(higherPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listenerCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ipLabel)
                    .addComponent(ipField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lowerPortLabel)
                    .addComponent(lowerPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(higherPortLabel)
                    .addComponent(higherPortSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void listenerCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listenerCheckBoxActionPerformed
        boolean selected = listenerCheckBox.isSelected();
        ipLabel.setEnabled(selected);
        lowerPortLabel.setEnabled(selected);
        higherPortLabel.setEnabled(selected);
        ipField.setEnabled(selected);
        lowerPortSpinner.setEnabled(selected);
        higherPortSpinner.setEnabled(selected);
    }//GEN-LAST:event_listenerCheckBoxActionPerformed

    private void lowerPortSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_lowerPortSpinnerStateChanged
        int value = (Integer)lowerPortSpinner.getValue();
        ((SpinnerNumberModel)higherPortSpinner.getModel()).setMinimum(value);
        if (((Integer)higherPortSpinner.getValue()) < value) {
            higherPortSpinner.setValue(value);
        }
}//GEN-LAST:event_lowerPortSpinnerStateChanged
    
    public boolean isSelected() {
        return listenerCheckBox.isSelected();
    }
    
    public void setSelected(boolean selected) {
        listenerCheckBox.setSelected(selected);
    }
    
    public String getIpAddress() {
        return ipField.getText();
    }
    
    public void setIpAddress(String address) {
        ipField.setText(address);
    }
    
    public int getLowerPort() {
        return (Integer)lowerPortSpinner.getValue();
    }
    
    public int getHigherPort() {
        return (Integer)higherPortSpinner.getValue();
    }
    
    public void setLowerPort(Integer port) {
        lowerPortSpinner.setValue(port);
    }
    
    public void setHigherPort(Integer port) {
        higherPortSpinner.setValue(port);
    }
    
    @Override
    public String getName() {
        return type;
    }
    
    private String type;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel higherPortLabel;
    private javax.swing.JSpinner higherPortSpinner;
    private javax.swing.JTextField ipField;
    private javax.swing.JLabel ipLabel;
    private javax.swing.JCheckBox listenerCheckBox;
    private javax.swing.JLabel lowerPortLabel;
    private javax.swing.JSpinner lowerPortSpinner;
    // End of variables declaration//GEN-END:variables
    
}
